package org.zheng.shop.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.zheng.shop.entity.Carousel;

import java.util.List;

/**
 * 轮播图Repository
 */
@Repository
public interface CarouselRepository extends JpaRepository<Carousel, Long> {
    
    /**
     * 查找所有启用的轮播图，按排序顺序排列
     */
    List<Carousel> findByIsEnabledTrueOrderBySortOrderAsc();
    
    /**
     * 查找所有轮播图，按排序顺序排列
     */
    List<Carousel> findAllByOrderBySortOrderAsc();
    
    /**
     * 查找指定排序顺序的轮播图数量
     */
    long countBySortOrder(Integer sortOrder);
    
    /**
     * 查找最大排序顺序
     */
    @Query("SELECT COALESCE(MAX(c.sortOrder), 0) FROM Carousel c")
    Integer findMaxSortOrder();
}
